// pages/my_setPhone/my_setPhone.js
const app = getApp()
var url = require('../../utils/url.js');
var token;
var userinfo;
var interval = null ;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo:"",
    phone:"",
    oldphone:"",
    time: '获取验证码', //倒计时 
    currentTime: 60,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
     wx.getStorage({
       key: 'token',
       success: function(res) {
         token=res.data
       },
     })
      wx.getStorage({
        key: 'userinfo',
        success: function (res) {
          userinfo = res.data;
          var phone=userinfo.phone
          var str2 = phone.substr(0, 3) + "****" + phone.substr(7);

          that.setData({
            userinfo: userinfo,
            phone: str2
          })
        },
      })
  },
  getCode:function(){
    var that=this;
    let phone = that.data.phone;
    if (phone.length==0){
       wx.showToast({
         title: '请输入手机号',
         icon: 'none',
       })
    }else{
      wx.request({
        url: url.URl + 'sms/send',
        data: {phone:phone},
        method: 'post',
        header: {
          'Authorization': token
        },
        success: function (res) {
          console.log(res)
          wx.hideLoading()
          var msg = res.data.msg
          if (res.data.code == 200) {
            that.getCode1();
            wx.showToast({
              title: '发送成功',
              icon: 'none',
            })
          }else{
            wx.showToast({
              title: msg,
              icon: 'none',
            })
          }
        }
      })
    }

  }, 
  getCode1: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 60,
          disabled: false
        })
      }
    }, 1000)
  },
  phone:function(e){
    this.setData({
      phone:e.detail.value
    })
  },
  code: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'userinfo',
      success: function (res) {
        userinfo = res.data;
        var oldphone = userinfo.phone;
        that.setData({
          oldphone: oldphone
        })
      },
    })
  },
  getphone() {
    var that=this;
    var phone = that.data.phone;
    var code = that.data.code;
    if(phone.length==0){
      wx.showToast({
        title: '请填写手机号',
        icon: 'none',
      })
    }else if(code.length==0){
      wx.showToast({
        title: '请填写验证码',
        icon:"none"
      })
    }else{
      wx.request({
        url: url.URl + 'my/profile/phone',
        data: { phone: phone, code: code },
        method: 'post',
        header: {
          'Authorization': token
        },
        success: function (res) {
          console.log(res)
          wx.hideLoading()
          var msg = res.data.msg
          if(res.data.code==200){

            userinfo.phone = phone
            wx.setStorage({
              key: 'userinfo',
              data: userinfo,
            })
            wx.showToast({
              title: msg,
              icon: 'none',
              success:function(){
                wx.navigateBack({
                  delta: 1,
                })
              }
            })
          }else{
            wx.showToast({
              title: msg,
              icon: 'none'
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})